package com.ynkbny.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author juzipi123
 * @date
 */
@Data
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = -3720998571176536865L;

    /**
     * 数据条数
     */
    private List<T> records = new ArrayList<>();

    /**
     * 一共有多少条数据
     */
    private long total;

    /**
     * 当前页数
     */
    private long currentPage;

    /**
     * 一页数据最多有多少条数据
     */
    private long pageSize;

    /**
     * 一共有多少页
     */
    private long totalPages;

    /**
     * 当前页数有多少条数据
     */
    private int numberOfElements;

    public PageResult() {
    }

    /**
     * 构造方法，只用把原来的page类放进来即可
     *
     * @param page 查出来的分页对象
     */
    public PageResult(Page<T> page) {
        this.records = page.getRecords();
        this.total = page.getTotal();
        this.currentPage = page.getCurrent();
        this.pageSize = page.getSize();
        this.numberOfElements = page.getRecords().size();
        this.totalPages = page.getPages();
    }

    /**
     * 构造方法，只用把原来的page类放进来即可
     *
     * @param records 查出来的分页对象
     */
    public PageResult(List<T> records, long count, long pageNum, long pageSize) {
        this.records = records;
        this.total = count;
        this.currentPage = pageNum;
        this.pageSize = pageSize;

        long l = count % pageSize;
        long m = count / pageSize;

        this.totalPages = m;
        if (l != 0) {
            this.totalPages += 1;
        }

        this.numberOfElements = Integer.parseInt(String.valueOf(pageSize));
        if (this.totalPages == this.currentPage && l != 0) {
            this.numberOfElements = Integer.parseInt(String.valueOf(l));
        }
    }

}
